package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Djsf;
import com.hopu.pojo.Emp;
import com.hopu.service.DjsfService;
import com.hopu.util.DateFormatGetTime;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@RequestMapping("/djsf")
@RestController
public class DjsfController {
    @Resource
    private DjsfService djsfServiceImpl;

    @PostMapping("/findAllDjsf")
    private ResultUtils findAllDjsf(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Djsf> allDjsf = djsfServiceImpl.findAllDjsf();

            PageInfo pageInfo = new PageInfo(allDjsf);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }
    @PostMapping("/findAllSfByMid")
    public ResultUtils findAllSfByMid(int mid,@RequestParam(value = "current",defaultValue = "1") Integer current){
            PageHelper.startPage(current,6);
        List<Djsf> djsf = djsfServiceImpl.findAllSfByMid(mid);
        PageInfo pageInfo = new PageInfo(djsf);
        return new ResultUtils(200,"查询页面成功",pageInfo);

    }
    @PostMapping("/addSfXf")
    public ResultUtils addSfXf(
            HttpSession session,
            @RequestParam(value = "mid",required = true)int mid,
                               @RequestParam(value = "myzj",required = true)Double myzj,
                               @RequestParam(value = "mbegintime")String mbegintime){


        try {

            Emp emp = (Emp) session.getAttribute("empLog");
            int num = djsfServiceImpl.addSfXf(mid,myzj,emp.getEid(),DateFormatGetTime.getTime(mbegintime));

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",null);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            return null;

    }
}
